package cn.entity;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class TeachView extends JFrame {
    private JLabel imageLabel;
    private int currentIndex = 0;
    private static final int TOTAL_IMAGES = 6;
    private static final String IMAGE_DIRECTORY = "img/teach/";
    private static final String IMAGE_NAME_PATTERN = "%d.png";

    public TeachView() {
        initializeUI();
        loadImage();
        setupMouseListener();
    }

    private void initializeUI() {
        setTitle("教程");
        setSize(800, 600);

        setLocationRelativeTo(null);

        setLayout(new BorderLayout());

        imageLabel = new JLabel("", JLabel.CENTER);
        imageLabel.setBackground(Color.BLACK);
        imageLabel.setOpaque(true);

        add(imageLabel, BorderLayout.CENTER);

        setVisible(true);
    }

    private void loadImage() {

        String imagePath = IMAGE_DIRECTORY +
                String.format(IMAGE_NAME_PATTERN, currentIndex + 1);

        File imageFile = new File(imagePath);

        if (!imageFile.exists()) {

            imageLabel.setText("图片加载失败: " + imagePath);
            return;
        }

        ImageIcon icon = new ImageIcon(imagePath);


        if (icon.getImage() != null) {

            int maxWidth = getWidth();
            int maxHeight = getHeight();

            Image original = icon.getImage();
            double scale = Math.min(
                    (double)maxWidth / original.getWidth(null),
                    (double)maxHeight / original.getHeight(null)
            );

            int scaledWidth = (int)(original.getWidth(null) * scale);
            int scaledHeight = (int)(original.getHeight(null) * scale);

            Image scaled = original.getScaledInstance(
                    scaledWidth,
                    scaledHeight,
                    Image.SCALE_SMOOTH
            );
            imageLabel.setIcon(new ImageIcon(scaled));
        }
    }

    private void setupMouseListener() {
        imageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                currentIndex++;
                if (currentIndex >= TOTAL_IMAGES) {
                    dispose();
                } else {
                    loadImage();
                }
            }
        });
    }


}